package cn.flying.system.web.cm;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.flying.base.common.vo.PageRequest;
import cn.flying.base.common.vo.PageResponse;
import cn.flying.base.common.vo.Rt;
import cn.flying.system.common.enums.TypeEnum;
import cn.flying.system.domain.cm.CmDictCode;
import cn.flying.system.domain.cm.CmDictType;
import cn.flying.system.service.cm.CmDictCodeService;
import cn.flying.system.service.cm.CmDictTypeService;


/**
 * @description:
 * @author: lvyong
 * @date: 2022年05月25日 19:21
 * @version: 1.0
 */
@Controller
@RequestMapping("/dict/")
public class CmDictCodeController {

    @Autowired
    private CmDictCodeService cmDictCodeService;
    @Autowired
    private CmDictTypeService cmDictTypeService;

    /**
     * 列表页面
     *
     * @return
     */
    @RequestMapping("type/list")
    public String dictTypeList(Model model) {
        List<CmDictCode> statusList = cmDictCodeService.getDictCodeList(TypeEnum.TYPE_ENUM_STATUS.getType());
        model.addAttribute("statusList", statusList);
        List<CmDictType> dictTypeList = cmDictTypeService.getDictTypeList();
        model.addAttribute("dictTypeList", dictTypeList);
        return "sys/dict/type/list";
    }

    /**
     * 列表页面--表数据
     *
     * @param pageRequest
     * @return
     */
    @PostMapping(value = "type/getDictTypePageList")
    @ResponseBody
    public PageResponse<CmDictType> getDictTypePageList(PageRequest<CmDictType> pageRequest) {
        PageResponse<CmDictType> pageResponse = cmDictTypeService.getDictTypePageList(pageRequest);
        return pageResponse;
    }

    /**
     * 新增/修改页面
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("type/turn/edit")
    public String turnTypeEdit(Long id, Model model) {
        List<CmDictCode> statusList = cmDictCodeService.getDictCodeList(TypeEnum.TYPE_ENUM_STATUS.getType());
        model.addAttribute("statusList", statusList);
        if (null != id) {
            CmDictType cmDictType = cmDictTypeService.getDictTypeById(id);
            model.addAttribute("dictType", cmDictType);
        } else {
            model.addAttribute("dictType", new CmDictType());
        }
        return "sys/dict/type/edit";
    }

    /**
     * 保存
     *
     * @param dictType
     * @return
     */
    @PostMapping(value = "type/save")
    @ResponseBody
    public Rt saveDictType(@RequestBody CmDictType dictType) {
        if (dictType.getId() == null) {
            cmDictTypeService.insertDictType(dictType);
        } else {
            cmDictTypeService.updateDictTypeById(dictType);
        }
        return Rt.success("保存成功！");
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @PostMapping("type/del")
    @ResponseBody
    public Rt delDictType(Long id) {
        cmDictTypeService.delDictType(id);
        return Rt.success("删除成功！");
    }

    /**
     * 列表页面
     *
     * @return
     */
    @RequestMapping("code/list")
    public String dictCodeList(String dictType, Model model) {
        List<CmDictCode> statusList = cmDictCodeService.getDictCodeList(TypeEnum.TYPE_ENUM_STATUS.getType());
        model.addAttribute("statusList", statusList);
        model.addAttribute("dictType", dictType);
        return "sys/dict/code/list";
    }

    /**
     * 列表页面--表数据
     *
     * @param pageRequest
     * @return
     */
    @PostMapping(value = "code/getDictCodePageList")
    @ResponseBody
    public PageResponse<CmDictCode> getDictCodePageList(PageRequest<CmDictCode> pageRequest) {
        PageResponse<CmDictCode> pageResponse = cmDictCodeService.getDictCodePageList(pageRequest);
        return pageResponse;
    }

    /**
     * 新增/修改页面（类型）
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("code/turn/edit")
    public String turnCodeEdit(Long id, String dictType, Model model) {
        if (null != id) {
            CmDictCode cmDictCode = cmDictCodeService.getDictCodeById(id);
            model.addAttribute("dictCode", cmDictCode);
        } else {
            CmDictCode dict = new CmDictCode();
            dict.setDictType(dictType);
            model.addAttribute("dictCode", dict);
        }
        List<CmDictType> typeList = cmDictTypeService.getDictTypeList();
        model.addAttribute("typeList", typeList);
        return "sys/dict/code/edit";
    }

    /**
     * 保存
     *
     * @param cmDictCode
     * @return
     */
    @PostMapping(value = "code/save")
    @ResponseBody
    public Rt saveDictCode(@RequestBody CmDictCode cmDictCode) {
        if (cmDictCode.getId() == null) {
            cmDictCodeService.insertDictCode(cmDictCode);
        } else {
            cmDictCodeService.updateDictCodeById(cmDictCode);
        }
        return Rt.success("保存成功！");
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @PostMapping(value = "code/del")
    @ResponseBody
    public Rt delDictCode(Long id) {
        cmDictCodeService.deleteDictCodeById(id);
        return Rt.success("删除成功！");
    }

    /**
     * 枚举下拉
     *
     * @param type
     * @return
     */
    @RequestMapping("/code/getCodeList")
    @ResponseBody
    public List<CmDictCode> getCodeList(String type) {
        List<CmDictCode> list = cmDictCodeService.getDictCodeList(type);
        return list;
    }
}
